<?php

use think\migration\Migrator;

class CreateDeviceTables extends Migrator
{
    /**
     * 执行迁移
     */
    public function change()
    {
        // 创建设备表
        $this->createDeviceTable();
    }
    
    /**
     * 创建设备表
     */
    private function createDeviceTable()
    {
        $table = $this->table('checkin_device', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '门禁设备表',
        ]);
        
        $table->addColumn('store_id', 'integer', ['comment' => '所属门店ID'])
              ->addColumn('sn', 'string', ['limit' => 50, 'comment' => '设备序列号'])
              ->addColumn('name', 'string', ['limit' => 100, 'comment' => '设备名称'])
              ->addColumn('location', 'string', ['limit' => 200, 'null' => true, 'comment' => '设备位置'])
              ->addColumn('device_type', 'string', ['limit' => 50, 'default' => 'face', 'comment' => '设备类型(face人脸,card刷卡,mixed混合)'])
              ->addColumn('passwd', 'string', ['limit' => 32, 'comment' => '设备密码(MD5)'])
              ->addColumn('ip_address', 'string', ['limit' => 50, 'null' => true, 'comment' => '设备IP地址'])
              ->addColumn('firmware_version', 'string', ['limit' => 50, 'null' => true, 'comment' => '固件版本'])
              ->addColumn('protocol_version', 'string', ['limit' => 10, 'null' => true, 'comment' => '协议版本'])
              ->addColumn('last_login_time', 'datetime', ['null' => true, 'comment' => '最后登录时间'])
              ->addColumn('last_heartbeat_time', 'datetime', ['null' => true, 'comment' => '最后心跳时间'])
              ->addColumn('online_status', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '在线状态(1在线,0离线)'])
              ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '设备状态(1启用,0禁用)'])
              ->addColumn('config', 'text', ['null' => true, 'comment' => '设备配置(JSON)'])
              ->addColumn('remark', 'string', ['limit' => 500, 'null' => true, 'comment' => '备注'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
              ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
              ->addIndex(['sn'], ['unique' => true])
              ->addIndex(['store_id'])
              ->addIndex(['status'])
              ->addIndex(['online_status'])
              ->create();
    }
}
